<template>
  <div class="v-container">
    <v-header title="个人中心">
      <span class="back" @click="goBack">
        <i class="el-icon-arrow-left"></i>
      </span>
    </v-header>
    <div class="mt">
      <img src="@/assets/images/login_img.jpg" width="100%">
      <div class="login-box">
        <div
          class="login-title"
          style="text-align:center;padding: 30px 20px;background-color: #fff;"
        >
          <h3 style="color:#333">欢迎您! {{this.userInfo.userName}}</h3>
          <div style="color:#333;margin-top:5px">手机号: {{this.userInfo.telephone | telephone}}</div>
          <div style="color:#333">车牌号: {{this.userInfo.carId}}</div>
          <div style="color:#333">尊享权益有效期截止至：{{this.userInfo.serviceEndTime}}</div>
        </div>

        <a href="tel:4008801768">
          <div class="login-title" style="padding:10px 20px;background-color:#fff;margin-top:10px;position:relative">
            <i class="person"></i>
            <p style="display: inline-block;position:relative;top:-8px;left:8px;color:#333">咨询服务</p>

            <p style="display: inline-block;position:absolute;right:15px;right:18px;top:17px;color:#333" >4008801768-3</p>
          </div>
        </a>
      </div>

      <div class="button-box">
        <p @click="logout">退出登录</p>
      </div>
    </div>
  </div>
</template>

<script>
import vHeader from "@/components/Header.vue";
import { Resource } from "@/services/resource";
import LocalStorage from "@/services/storage";
import { MessageBox, Toast } from "mint-ui";

export default {
  name: "activity",
  components: {
    vHeader
  },
  data() {
    return {
      userInfo: {
        telephone: "",
        userName: ""
      }
    };
  },
  mounted() {
    this.getUserInfo();
  },
  methods: {
    // 返回上一页
    goBack() {
      this.$router.go(-1);
    },
    // 获取用户信息
    getUserInfo() {
      let loginStatus = LocalStorage.getLocalStorage('loginStatus');
      Resource.userInfo
        .post({}, { matrixContent: loginStatus,needToken:true })
        .then(res => {
          let { telephone, userName, carId, serviceEndTime } = res.body;
          this.telephone = res.body.telephone;
          Object.assign(this.userInfo, {
            telephone,
            userName,
            carId,
            serviceEndTime
          });
          this.userInfo.serviceEndTime = this.userInfo.serviceEndTime.slice(0, 11)
        });
    },
    // 退出登录
    logout() {
      const msgBox = MessageBox({
        title: "提示",
        message: "确定退出?",
        showCancelButton: true
      });
      let loginStatus = LocalStorage.getLocalStorage('loginStatus');
      msgBox.then(action => {
        if (action === "confirm") {
          Resource.loginOut
            .post({}, { matrixContent: loginStatus })
            .then(res => {
              console.log(res);
              Toast({
                message: "退出成功",
                iconClass: "el-icon-success"
              });
              LocalStorage.delLocalStorage("loginStatus");
              LocalStorage.delLocalStorage("openid");
              LocalStorage.delLocalStorage("token");
              
              LocalStorage.delLocalStorage("userInfo");
              this.$store.dispatch("logout");
              this.$router.push("login");
            });
        }
      });
    }
  },
  filters: {
    telephone: function(value) {
      console.log(value);
      return value.substring(0, 3) + "****" + value.substring(7, 11);
    },
    userName: function(value) {
      let tmp = value.substring(4, value.length - 4);
      let tmp2 = "";
      for (let i = 0; i < tmp.length; i++) {
        tmp2 += "*";
      }
      value =
        value.substring(0, 4) +
        tmp2 +
        value.substring(value.length - 4, value.length);
      return value;
    }
  }
};
</script>

<style lang="less" scoped>
@import url("./personal.less");
</style>
